const Log = require('ee-core/log');
const sequelize = require('sequelize');
const register = require('./model')
/**
 * Sequelize
 * @class
 */
class Sequelize {

    constructor(app) {
        this.app = app;
    }

    /**
     * 创建托盘
     */
    async create() {
        console.log(this.app)
        this.app.Seque = new sequelize('weblog', 'root', 'zzq199905!!', {
            host: '140.143.235.209',
            pool: {
                max: 5,
                min: 0,
                acquire: 30000,
                idle: 10000
            },
            dialect: 'mysql'

        },);
        await this.testConn()
        await this.registerModel()
    }

    async testConn() {
        try {
            await this.app.Seque.authenticate()
            Log.info('[Sequelize] has been established successfully.');
        } catch (e) {
            console.log(e)
            Log.info('[Sequelize] Unable to connect to the database');
        }
    }

    async registerModel() {
        register(sequelize,this.app.Seque)
        Log.info('[Sequelize:model] has been registered successfully.');
    }

}

Sequelize.toString = () => '[class Sequelize]';
module.exports = Sequelize;